<template>
  <div id="auth">
    <div class="wrapper">
      <div class="back_part">
        <div class="back_btn" @click="goBack">
          <img src="@/assets/images/pc_black/auth/back.png" />
          <span>返回</span>
        </div>
      </div>

      <div class="discribe_part">
        <span>实名认证</span>
        <span class="test"
          >按照法律法规要求，进一步使用区块链服务需要提供身份信息，请填写真实、有效的信息。</span
        >
        <span class="test"
          >实名认证后即可创建区块链地址并支付购买，请确认注册手机号与实名信息匹配。</span
        >
      </div>

      <div class="form_part">
        <div class="form_box">
          <label>
            <span>真实姓名</span>
            <input
              class="nameInput"
              v-model.trim="formData.realName"
              type="text"
              placeholder="请输入您的真实姓名"
              @input="realNameInputing"
            />
          </label>
          <label>
            <span>身份证号</span>
            <input
              class="idInput"
              v-model.trim="formData.IDCard"
              type="tel"
              placeholder="请输入您的身份证号"
              @input="IDCardInputing"
            />
          </label>
        </div>

        <div class="btn_box">
          <button v-if="cantBtn" class="btn_grey" :disabled="cantBtn">
            提交
          </button>
          <button
            v-else
            class="btn_color"
            :disabled="cantBtn"
            @click="openConfirmDialog"
          >
            提交
          </button>
        </div>
      </div>

      <!-- <div class="bottom_part">
        <div class="font_box">
          <span
            >Copyright © 2022 海南原上区块链科技有限公司 All Rights Reserved.
          </span>
          <span>琼ICP备18003326号-3</span>
        </div>
      </div> -->

      <el-dialog
        class="confirmDialog"
        :visible="isShowConfirmDialog"
        width="30%"
        :before-close="closeConfirmDialog"
        :show-close="false"
        center
        :close-on-click-modal="false"
      >
        <div class="dialog_title">
          <div class="left_box">
            <img
              class="icon_left"
              src="@/assets/images/pc_black/icons/icon_warning.png"
            />
            <span>确认认证吗？</span>
          </div>

          <img
            class="icon_right"
            @click="closeConfirmDialog"
            src="@/assets/images/pc_black/auth/close.png"
          />
        </div>

        <div class="dialog_center">
          <span>实名信息认证后不可修改，请核验信息无误后继续。</span>
        </div>

        <div class="dialog_bottom">
          <el-button @click="closeConfirmDialog">返回修改</el-button>
          <el-button @click="userAuth">继续认证</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// vuelidate 用于表单验证
import { required } from "vuelidate/lib/validators";
import { mapActions } from "vuex";
// api
import { userAuthApi } from "@/api";
import { iscode } from "@/utils";

export default {
  name: "Auth",
  data() {
    return {
      // form data
      formData: {
        realName: "",
        IDCard: "",
      },
      // btn disabled
      cantBtn: true,
      // show confirm dialog
      isShowConfirmDialog: false,
    };
  },
  // vuelidate
  validations: {
    formData: {
      realName: {
        required,
      },
      IDCard: {
        required,
      },
    },
  },
  methods: {
    // ------ vuex ------
    // 获取用户信息（用于认证之后更新vuex里的userInfo数据）
    ...mapActions(["getUserInfo"]),

    // ------ async ------
    // 身份认证
    async userAuth() {
      let result = await userAuthApi({
        realname: this.formData.realName,
        idcard: this.formData.IDCard,
      });
      if (iscode(result)) {
        this.$message({
          message: "认证成功",
          iconClass: "mySuccessIcon",
          customClass: "mySuccess",
        });
        this.closeConfirmDialog();
        this.getUserInfo();
        this.initForm();
        // 如果商品id参数存在，则跳转到响应页面
        if (this.$route.query.id) {
          this.$router.push({
            path: "/mail/productDetail",
            query: {
              id: this.$route.query.id - 0,
            },
          });
        } else {
          this.$router.push("/personalCenter");
        }
      }
    },

    // ------ common ------
    // 用于初始化清空表单残留
    initForm() {
      this.formData = {
        realName: "",
        IDCard: "",
      };
      this.cantBtn = true;
    },
    // 打开确认认证弹窗
    openConfirmDialog() {
      this.isShowConfirmDialog = true;
    },
    // 关闭确认认证弹窗
    closeConfirmDialog() {
      this.isShowConfirmDialog = false;
    },

    // ------ form ------
    // 姓名输入框输入时
    realNameInputing() {
      if (!this.$v.$invalid) {
        this.cantBtn = false;
      } else {
        this.cantBtn = true;
      }
    },
    // 身份证输入框输入时
    IDCardInputing() {
      this.formData.IDCard = this.formData.IDCard.replace(/[\W]/g, "");

      if (!this.$v.$invalid) {
        this.cantBtn = false;
      } else {
        this.cantBtn = true;
      }
    },

    // ------ gotoPage ------
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="less" scoped>
#auth {
  position: absolute;
  left: 0;
  right: 0;
  top: 60px;
  bottom: 0;
  background: #171818;

  .wrapper {
    width: 1200px;
    margin: 0 auto;
  }

  // >> 返回按钮部分
  .back_part {
    width: 100%;
    padding-top: 20px;

    .back_btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 104px;
      height: 30px;
      border: none;
      background: none;
      background-image: url("../../assets/images/personalCenter/btn.png");
      background-position: center center;
      background-size: 100% 100%;
      cursor: pointer;

      img {
        width: 20px;
        height: 6px;
      }

      span {
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        line-height: 12px;
        margin-left: 10px;
      }
    }
  }

  // >> 描述信息部分
  .discribe_part {
    display: flex;
    flex-direction: column;
    padding-top: 30px;

    span {
      font-family: PingFangSC-Regular, PingFang SC;
      color: #999999;
      font-weight: 400;
      font-size: 14px;
      line-height: 14px;
    }

    span:nth-child(1) {
      font-family: PingFangSC-Semibold, PingFang SC;
      font-size: 18px;
      font-weight: 600;
      color: #ffffff;
      line-height: 18px;
    }

    span:nth-child(2) {
      margin: 20px 0 15px 0;
    }
  }
  // >> 表单部分
  .form_part {
    padding-top: 40px;

    .form_box {
      display: flex;
      flex-direction: column;

      label {
        display: flex;
        align-items: center;
        height: 42px;
        padding: 0;
        margin: 0;

        span {
          font-size: 16px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
          line-height: 16px;
        }

        input {
          width: 300px;
          height: 42px;
          background: #1e2020;
          border: 1px solid #444444;
          padding-left: 10px;
          font-size: 14px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
          line-height: 14px;
          margin-left: 23px;
        }
        input::-webkit-input-placeholder {
          /* WebKit browsers 适配谷歌 */
          color: #555555 !important;
        }
        input:-moz-placeholder {
          /* Mozilla Firefox 4 to 18 适配火狐 */
          color: #555555 !important;
        }
        input::-moz-placeholder {
          /* Mozilla Firefox 19+ 适配火狐 */
          color: #555555 !important;
        }
        input:-ms-input-placeholder {
          /* Internet Explorer 10+  适配ie*/
          color: #555555 !important;
        }
      }

      label:nth-child(2) {
        margin-top: 15px;
      }
    }

    .btn_box {
      padding-top: 30px;
      padding-left: 87px;

      button {
        width: 215px;
        height: 39px;
        border: none;
        background: none;
        background-size: 100% 100%;
        background-position: center center;
        font-size: 14px;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #1e2020;
        line-height: 14px;
      }
      .btn_color {
        background-image: url("../../assets/images/pc_black/auth/btn_color.png");
      }
      .btn_grey {
        background-image: url("../../assets/images/pc_black/auth/btn_grey.png");
      }
    }
  }

  // >> 底部部分
  .bottom_part {
    position: absolute;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 122px;
    background: #171818;
    box-shadow: 0px -1px 0px 0px #2e2f2f;

    .font_box {
      display: flex;
      flex-direction: column;

      span {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #999999;
        line-height: 14px;
        text-align: center;
      }

      span:nth-child(2) {
        margin-top: 15px;
      }
    }
  }

  // 绑定手机号弹窗
  .confirmDialog {
    /deep/.el-dialog {
      max-width: 480px;
      height: 220px;
      background: #1e2020;
      border-radius: 4px;

      .el-dialog__header {
        display: none;
      }

      .el-dialog__body {
        margin: 0;
        padding: 0;

        .dialog_title {
          display: flex;
          align-items: center;
          justify-content: space-between;
          width: 100%;
          padding: 20px 20px 0 30px;

          .left_box {
            display: flex;
            align-items: center;
            transform: translateY(10px);
            .icon_left {
              width: 20px;
              height: 20px;
            }

            span {
              font-size: 16px;
              font-family: PingFangSC-Medium, PingFang SC;
              font-weight: 500;
              color: rgba(255, 255, 255, 0.85);
              line-height: 16px;
              margin-left: 10px;
            }
          }

          .icon_right {
            width: 15px;
            height: 15px;
            cursor: pointer;
          }
        }

        .dialog_center {
          width: 100%;
          padding: 33px 0 0 60px;
          font-size: 14px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #999999;
          line-height: 14px;
        }

        .dialog_bottom {
          display: flex;
          justify-content: flex-end;
          width: 100%;
          position: absolute;
          bottom: 0;
          left: 0;
          padding: 0 15px 10px 0;

          .el-button {
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0;
            padding: 0;
            margin-left: 10px;
            background: none;
            border: none;
            width: 96px;
            height: 32px;
            border-radius: 3px;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            line-height: 14px;
          }

          .el-button:nth-child(1) {
            border: 1px solid rgba(221, 221, 221, 0.5);
            font-weight: 400;
            color: #999999;
          }
          .el-button:nth-child(2) {
            background: #00ffb0;
            font-weight: 500;
            color: #1e2020;
          }
        }

        // 双击不选中
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        -khtml-user-select: none;
        user-select: none;
      }
    }
  }
  /deep/.el-dialog__wrapper {
    background: rgba(0, 0, 0, 0.5);
  }
}
</style>
